package cn.software.service.impl;


import cn.software.entity.Topic;
import cn.software.entity.param.TopicParam;
import cn.software.entity.vo.SubjectVo;
import cn.software.entity.vo.TopicVo;
import cn.software.mapper.TopicMapper;
import cn.software.service.SubjectService;
import cn.software.service.TopicService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    private TopicMapper topicMapper;
    @Autowired
    private SubjectService subjectService;

    @Override
    public boolean addTopic(TopicParam topicParam) {
        Topic topic = transformTopicParamToTopic(topicParam);
        int insert = topicMapper.insert(topic);
        return insert > 0;
    }


    @Override
    public List<TopicVo> listTopic(Integer subjectId) {
        QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("subject_id", subjectId);
        queryWrapper.eq("is_deleted", 0);
        List<Topic> topicList = topicMapper.selectList(queryWrapper);
        // 根据科目id返回科目具体信息
        SubjectVo subjectVo = subjectService.getSubjectById(subjectId);
        if(subjectVo == null) {
            return null;
        }
        List<TopicVo> result = transformTopicListToVo(topicList, subjectVo);
        return result;
    }


    @Override
    public List<TopicVo> listAllTopic(Integer curPage) {
        QueryWrapper<Topic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.last("limit "+curPage+",15");
        List<Topic> topicList = topicMapper.selectList(queryWrapper);
        List<TopicVo> topicVoList = transformTopicListToVo(topicList, null);
        return topicVoList;
    }

    @Override
    public boolean editTopic(TopicParam topicParam) {
        Topic topic = transformTopicParamToTopic(topicParam);
        int update = topicMapper.updateById(topic);
        return update > 0;
    }

    @Override
    public boolean deleteTopic(Integer topicId) {
        int delete = topicMapper.deleteById(topicId);
        return delete > 0;
    }

    @Override
    public TopicVo getTopic(Integer id) {
        Topic topic = topicMapper.selectById(id);
        if(topic == null || topic.getIsDeleted() == 1) {
            return null;
        }
        TopicVo topicVo = transformTopicToVo(topic, null);

        return topicVo;
    }

    Map<Integer, SubjectVo> visited = new HashMap<>();
    private List<TopicVo> transformTopicListToVo(List<Topic> topicList, SubjectVo subjectVo) {
        List<TopicVo> list = new ArrayList<>();
        for (Topic topic : topicList) {
            list.add(transformTopicToVo(topic, subjectVo));
        }
        return list;
    }



    private TopicVo transformTopicToVo(Topic topic, SubjectVo subjectVo) {
        /**
         * 如果subjectVo为空,就不是按照科目查询的题目
         * 所以每个题目对应的科目是乱序的
         * 就对每个题目分别查询
         * 另外，定义一个map，对已经查询到的就不再查询了
        */

        Integer subjectId = topic.getSubjectId();
        if(subjectVo == null) {
            if(visited.containsKey(subjectId)) {
                subjectVo = visited.get(subjectId);
            } else {
                subjectVo = subjectService.getSubjectById(subjectId);
                visited.put(subjectId, subjectVo);
            }
        } else {
            visited.put(subjectId, subjectVo);
        }



        /**
         * 具体转化过程
         */
        TopicVo topicVo = new TopicVo();
        BeanUtils.copyProperties(topic, topicVo);
        topicVo.setOption(topic.getOption());
        topicVo.setSubjectVo(visited.get(subjectId));
        return topicVo;
    }

    /**
     * 将topicParam转化为topic
     * @param topicParam
     * @return
     */
    private Topic transformTopicParamToTopic(TopicParam topicParam) {
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicParam, topic);
        String[] options = topicParam.getOption();
        // 将选项去除空格后，用空格作为分界，拼成字符串
        StringBuilder sb = new StringBuilder();
        for (String option : options) {
            sb.append(option.replaceAll(" ", ""));
            sb.append(" ");
        }
        topic.setOption(new String(sb));
        return topic;
    }
}
